package org.tokaf.datasearcher;

import java.io.InputStream;

import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.util.FileManager;

/**
 * <p>JenaMemory DataSearcher, which works with file. It creates virtual
 * database in memory and load content of the file in it.</p> <p>Copyright (c)
 * 2006</p>
 * @author Alan Eckhardt
 * @version 1.0
 */
public class JenaMemoryFileSearcher extends JenaMemorySearcher {
	public JenaMemoryFileSearcher() {
	}

	public Object clone() {
		JenaMemoryFileSearcher newInstance = new JenaMemoryFileSearcher();
		newInstance.initSource(this);
		newInstance.initSearchTime = this.initSearchTime;
		newInstance.initSourceTime = this.initSourceTime;
		newInstance.norm = this.norm;
		newInstance.name = this.name;

		newInstance.columnCount = this.columnCount;
		newInstance.index = this.index;
		newInstance.results = this.results;
		newInstance.searchIndex = this.searchIndex;
		return newInstance;
	}

	/**
	 * initSource
	 * @param db String
	 * @return int
	 */
	public int initSource(String db) {
		long startTime = System.nanoTime();

		model = ModelFactory.createDefaultModel();
		// use the FileManager to find the input file
		InputStream in = FileManager.get().open(db);
		if (in == null) {
			throw new IllegalArgumentException("File: " + db + " not found");
		}
		model.read(in, "");
		initSourceTime += System.nanoTime() - startTime;
		return 1;
	}
}
